<template>
  <div class="setting-container">
    <div class="app-container">
      <!-- 卡片组件 -->
      <el-card class="box-card">
        <!-- 使用 Tabs 组件完成标签页布局 -->
        <el-tabs v-model="activeName">
          <el-tab-pane label="角色管理" name="first" class="tab-pane">
            <!-- 新增角色按钮 -->
            <el-row style="height: 60px">
              <el-button
                icon="el-icon-plus"
                size="small"
                type="primary"
                @click="addbtn"
              >新增角色</el-button>
            </el-row>
            <!-- 使用 Table 组件实现用户角色的渲染 -->
            <el-table border :data="rolesList" style="width: 100%">
              <el-table-column type="index" label="序号" width="120" />
              <el-table-column label="角色名" prop="name" width="240" />
              <el-table-column label="描述" prop="description" />
              <el-table-column label="操作">
                <template slot-scope="scope">
                  <el-button
                    size="small"
                    type="success"
                    @click="setRoles(scope.row.id)"
                  >分配权限</el-button>
                  <el-button
                    size="small"
                    type="primary"
                    @click="editRoles(scope.row.id)"
                  >编辑</el-button>
                  <el-button
                    size="small"
                    type="danger"
                    @click="delRoles(scope.row.id)"
                  >删除</el-button>
                </template>
              </el-table-column>
            </el-table>

            <!-- 分页区域 -->
            <el-pagination
              :current-page="query.page"
              :page-sizes="[10, 15, 20, 25]"
              :page-size="query.pagesize"
              layout="total, sizes, prev, pager, next, jumper"
              :total="total"
              @size-change="handleSizeChange"
              @current-change="handleCurrentChange"
            />
            <!-- 公司信息页面 -->
          </el-tab-pane>
          <el-tab-pane label="公司信息" class="tab-pane">
            <el-alert
              title="对公司名称、公司地址、营业执照、公司地区的更新，将使得公司资料被重新审核，请谨慎修改"
              type="info"
              show-icon
              :closable="false"
            />
            <el-form label-width="120px" style="margin-top: 50px">
              <el-form-item label="公司名称">
                <el-input
                  v-model="formData.name"
                  disabled
                  style="width: 400px"
                />
              </el-form-item>
              <el-form-item label="公司地址">
                <el-input
                  v-model="formData.companyAddress"
                  disabled
                  style="width: 400px"
                />
              </el-form-item>
              <el-form-item label="邮箱">
                <el-input
                  v-model="formData.mailbox"
                  disabled
                  style="width: 400px"
                />
              </el-form-item>
              <el-form-item label="备注">
                <el-input
                  v-model="formData.remarks"
                  type="textarea"
                  :rows="3"
                  disabled
                  style="width: 400px"
                />
              </el-form-item>
            </el-form>
          </el-tab-pane>

          <!-- 新增弹框 -->
          <el-dialog
            :title="isedit ? '编辑角色' : '新增角色'"
            :close-on-click-modal="false"
            :close-on-press-escape="false"
            :visible.sync="showDialog"
            :before-close="handleClose"
          >
            <el-form
              ref="roleForm"
              :model="roleForm"
              :rules="rules"
              label-width="100px"
              reset-fields
            >
              <el-form-item label="角色名称" prop="name">
                <el-input v-model="roleForm.name" />
              </el-form-item>
              <el-form-item label="角色描述" prop="description">
                <el-input v-model="roleForm.description" />
              </el-form-item>
            </el-form>

            <!-- 底部 -->
            <el-row slot="footer" type="flex" justify="center">
              <el-col :span="6">
                <el-button size="small" @click="cancleRoles">取消</el-button>
                <el-button
                  size="small"
                  type="primary"
                  @click="Submit"
                >确定</el-button>
              </el-col>
            </el-row>
          </el-dialog>

          <!-- 分配权限弹窗 -->
          <el-dialog :visible.sync="assignshow" title="分配权限" width="50%">
            <!-- 分配权限组件 -->
            <assign-permission
              v-if="assignshow"
              :role-id="roleId"
              @close="setVisible"
              @closero="assignshow = false"
            />
          </el-dialog>
        </el-tabs>
      </el-card>
    </div>
  </div>
</template>

<script>
import {
  getRoles,
  getCompanyInfo,
  addRole,
  getRoleId,
  updateRole,
  deleteRole
} from '@/api/setting'
// 引入分配权限组件
import AssignPermission from './assignPermission.vue'
export default {
  components: {
    AssignPermission
  },
  data() {
    return {
      isedit: false,
      activeName: 'first',
      query: {
        page: 1, // 当前页面
        pagesize: 10 // 页面显示的条数
      },
      rolesList: [], // 角色列表
      total: 0, // 角色数据总条数
      formData: {}, // 公司信息数据
      // 下面是弹窗相关数据
      showDialog: false, // 弹窗显示隐藏
      roleForm: {
        name: '',
        description: ''
      },
      rules: {
        name: [{ required: true, message: '请输入角色名称', trigger: 'blur' }],
        description: [
          { required: true, message: '请输入角色描述', trigger: 'blur' }
        ]
      },
      assignshow: false, // 分配权限组件显示隐藏
      roleId: '' // 角色id
    }
  },
  created() {
    this.getRoles() // 获取全部角色信息的接口调用
    this.getCompanyInfo() // 获取公司信息的接口调用
  },
  methods: {
    // 分配权限按钮
    setRoles(id) {
      this.assignshow = true
      this.roleId = id
    },
    // 点击取消关闭弹窗
    setVisible() {
      this.assignshow = false
    },
    addbtn() {
      this.showDialog = true
      this.isedit = false
    },
    // 点击弹窗中的确定新增角色
    Submit() {
      this.$refs.roleForm.validate(async(valid) => {
        if (valid) {
          if (!this.isedit) {
            const res = await addRole(this.roleForm)
            this.$message.success(res.message)
          } else {
            const res = await updateRole(this.roleForm)
            this.$message.success(res.message)
          }
          this.getRoles()
          this.showDialog = false

          this.$refs.roleForm.resetFields()
        }
      })
    },
    // 获取全部角色信息
    async getRoles() {
      const res = await getRoles(this.query)
      this.rolesList = res.data.rows
      this.total = res.data.total
    },
    // 获取公司信息
    async getCompanyInfo() {
      const id = this.$store.getters.companyId
      const res = await getCompanyInfo(id)
      this.formData = res.data
    },
    // 编辑角色,数据回填
    async editRoles(id) {
      this.showDialog = true
      this.isedit = true
      if (this.isedit) {
        const res = await getRoleId(id) // 获取单个角色信息
        this.roleForm = res.data
      }
    },
    // 点击弹出层的小×号
    handleClose() {
      this.showDialog = false
      this.$refs.roleForm.resetFields()
    },
    // 点击取消按钮
    cancleRoles() {
      this.$refs.roleForm.resetFields()
      this.showDialog = false
    },
    // 每页显示的条数发生改变时触发
    handleSizeChange(newSize) {
      this.query.pagesize = newSize
      this.getRoles()
    },
    // 当前页面发生改变时触发
    handleCurrentChange(newPage) {
      this.query.page = newPage

      this.getRoles()
    },

    // 删除角色
    delRoles(id) {
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(async(err) => {
          if (err === 'confirm') {
            const res = await deleteRole(id)
            this.$message.success(res.message)
            if (this.rolesList.length === 1) {
              this.query.page--
              if (this.query.page <= 0) {
                this.query.page = 1
              }
            }
            this.getRoles()
          }
        })
        .catch((err) => {
          if (err === 'cancel') {
            this.$message('已经取消删除')
          }
        })
    }
  }
}
</script>

<style lang="scss" scoped>
.box-card {
  padding: 10px 30px;
}

.tab-pane {
  padding: 20px;
}

.el-pagination {
  text-align: center;
  margin-top: 20px;
}
</style>
